<?php

namespace App\Domain\PcHealthCheck\Controllers;

use App\Domain\FamilyMember\RpcService\FamilyMemberInterface;
use App\Domain\FamilyMember\RpcService\FamilyMemberService;
use App\Domain\HealthCheck\RpcServices\HealthCheckService;
use App\Domain\HealthCheck\RpcServices\HealthCheckServiceInterface;
use App\Http\Controllers\Controller;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

class PcHealthCheckController extends Controller
{
    public function getByName(Request $request, FamilyMemberInterface $familyMember)
    {
        $data = $familyMember->getByName($request['name']);
        return $this->success($data);
    }

    public function getByIdcard(Request $request, FamilyMemberInterface $familyMember)
    {
        $data = $familyMember->getByIdcard($request['idcard']);
        return $this->success($data);
    }

    public function getList(Request $request, HealthCheckServiceInterface $healthCheckService,FamilyMemberInterface $familyMember)
    {
        $params = [
            'name' => $request['name'],
            'id' => $request['id'],
            'idcard' => $request['idcard'],
            'type' => $request['type'],
            'start_time' => $request['start_time'],
            'end_time' => $request['end_time'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];
        $healthPaginator = $healthCheckService->getInfo($params);
        $items = array_map(function ($val) {
            if ($val['type'] == 0) {
                if ($val['value0'] <= 90) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] > 90 && $val['value0'] < 140) {
                    $val['value0Tag'] = '正常';
                }
                if ($val['value0'] >= 140) {
                    $val['value0Tag'] = '偏高';
                }
                if ($val['value1'] <= 60) {
                    $val['value1Tag'] = '偏低';
                }
                if ($val['value1'] > 60 && $val['value1'] < 90) {
                    $val['value1Tag'] = '正常';
                }
                if ($val['value1'] >= 90) {
                    $val['value1Tag'] = '偏高';
                }
                if ($val['value2'] <= 60) {
                    $val['value2Tag'] = '偏低';
                }
                if ($val['value2'] > 60 && $val['value2'] < 100) {
                    $val['value2Tag'] = '正常';
                }
                if ($val['value2'] >= 100) {
                    $val['value2Tag'] = '偏高';
                }
            }
            if ($val['type'] == 1) {
                if ($val['item'] == 0) {
                    if ($val['value0'] <= 3.6) {
                        $val['value0Tag'] = '偏低';
                    }
                    if ($val['value0'] > 3.6 && $val['value0'] < 6.1) {
                        $val['value0Tag'] = '正常';
                    }
                    if ($val['value0'] >= 6.1) {
                        $val['value0Tag'] = '偏高';
                    }
                }
                if ($val['item'] == 1) {
                    if ($val['value0'] <= 7.9) {
                        $val['value0Tag'] = '正常';
                    }
                }
            }
            if ($val['type'] == 2) {
                if ($val['value0'] <= 36) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] > 36 && $val['value0'] < 37.3) {
                    $val['value0Tag'] = '正常';
                }
                if ($val['value0'] >= 37 && $val['value0'] <= 38) {
                    $val['value0Tag'] = '发热';
                }
                if ($val['value0'] >= 38.1 && $val['value0'] <= 40) {
                    $val['value0Tag'] = '高烧';
                }
            }
            if ($val['type'] == 3) {
                $height = (new FamilyMemberService())->getInfo($val['uid']);
                $heightM = $height['height'] * 0.01;
                $wight = $heightM * $heightM * 22;
                if ($val['value0'] < $wight) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] > $wight) {
                    $val['value0Tag'] = '偏高';
                }
            }
            if ($val['type'] == 4) {
                if ($val['value0'] <= 95) {
                    $val['value0Tag'] = '偏低';
                }
                if ($val['value0'] >= 95) {
                    $val['value0Tag'] = '正常';
                }
            }
            return $val;
        }, $healthPaginator->items());
        $healthPaginator = new LengthAwarePaginator($items, $healthPaginator->total(), $healthPaginator->perPage(), $healthPaginator->currentPage());
        //格式化
        $keyMap = [
            'date' => 'create_time',
            'item' => 'item',
            'type' => 'type',
            'value0' => 'value0',
            'value1' => 'value1',
            'value2' => 'value2',
            'value0Tag' => 'value0Tag',
            'value1Tag' => 'value1Tag',
            'value2Tag' => 'value2Tag',
            'name' => 'name',
            'idCard' => 'idCard',
        ];
        return $this->success(ResponseFormatter::formatterPaginator($healthPaginator, $keyMap));
    }
}
